class Solution:
    def dictEqual(self, d1, d2):
        if len(d1) != len(d2):
            return False
        for k in d1:
            if k not in d2:
                return False
            if d1[k] != d2[k]:
                return False
        return True

    def checkInclusion(self, s1: str, s2: str) -> bool:
        if len(s1) > len(s2):
            return False
        if len(s1) == 0:
            return True
        
        s1_dict = {}
        for i in range(len(s1)):
            if s1[i] in s1_dict:
                s1_dict[s1[i]] += 1
            else:
                s1_dict[s1[i]] = 1
    
        s2_dict = {}
        for i in range(len(s1)):
            if s2[i] in s2_dict:
                s2_dict[s2[i]] += 1
            else:
                s2_dict[s2[i]] = 1

        left, right = 0, len(s1) - 1
        while right < len(s2):
            if self.dictEqual(s1_dict, s2_dict):
                return True
            left += 1
            right += 1
            if right == len(s2):
              return False
            if s2_dict[s2[left-1]] - 1 == 0:
                del s2_dict[s2[left-1]]
            else:
                s2_dict[s2[left-1]] -= 1
            if  s2[right] in s2_dict:
                s2_dict[s2[right]] += 1
            else:
                s2_dict[s2[right]] = 1
        return False
            